package com.yq.testmodule.tiktok

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.content.DialogInterface.OnShowListener
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.fmt.mvi.learn.gobal.log
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.yq.testmodule.R
import com.yq.testmodule.view.DragViewLayout
import config.TEST_TikTok


@Route(path = TEST_TikTok)
class TestTikTokActivity : AppCompatActivity() {

    lateinit var mBottomSheetFragment: MyBottomSheetDialog
    lateinit var mFlContent: FrameLayout
    var mOriginHeight = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_tiktok)
        mFlContent = findViewById(R.id.flContent)


        val dragViewLayout = findViewById<DragViewLayout>(R.id.dragView)

        dragViewLayout.setOpenRatioListener {

            var showHeight =
                resources.displayMetrics.heightPixels * 9 / 16  * it
            val layoutParams = mFlContent.layoutParams
            layoutParams.height = (mOriginHeight - showHeight).toInt()
            mFlContent.layoutParams = layoutParams

        }
        dragViewLayout.post {
            mOriginHeight = mFlContent.measuredHeight
            mOriginHeight.log()
        }
        findViewById<ImageView>(R.id.ivImage).setOnClickListener {
            dragViewLayout.open()
        }
        findViewById<ImageView>(R.id.ivImage).setOnLongClickListener {
            showBottomSheetFragment()
            return@setOnLongClickListener true
        }
        mBottomSheetFragment = MyBottomSheetDialog()
    }


    private fun showBottomSheetFragment() {
        mOriginHeight = mFlContent.measuredHeight
        mBottomSheetFragment.show(supportFragmentManager, "mybottomsheet")
        mBottomSheetFragment.setSlideCallback { slideOffset ->
            var showHeight =
                resources.displayMetrics.heightPixels / 2 * (Math.abs(slideOffset))
            val layoutParams = mFlContent.layoutParams
            layoutParams.height = (mOriginHeight - showHeight).toInt()
            mFlContent.layoutParams = layoutParams
        }

    }


}